package com.lcke.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lcke.common.config.LemonCnt;
import com.lcke.common.login.User;
import com.lcke.common.util.HttpContext;
import com.lcke.system.model.MenuResult;
import com.lcke.system.model.SystemUser;
import com.lcke.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("")
public class IndexController {

    @Autowired
    private MenuService menuService;

    /**
     * 主页
     *
     * @param request
     * @return
     */
    @RequestMapping({"/index", "/"})
    public String index(HttpServletRequest request) {
        SystemUser systemUser = null;
        User user = HttpContext.getUser();
        if (null == user || null == user.getUserId()) {
            return "login";
        }
        systemUser = BeanUtil.copyProperties(user, SystemUser.class);
        MenuResult menus = menuService.getUserMenus(systemUser);
        request.setAttribute("menus", menus);
        List<SystemUser> roles = systemUser.getRoles();
        request.setAttribute("roles", roles);
        request.setAttribute("user", systemUser);
        return "index";
    }

    /**
     * todo主页 看板
     */
    @RequestMapping("/index/main")
    public String indexmain() {
        return "pages/index/main.html";
    }

    @RequestMapping("/index/changeDs")
    public String changeDs(String ds) {
        if (LemonCnt.DS_KEY_ONE.equals(ds) || LemonCnt.DS_KEY_TWO.equals(ds)) {
            LemonCnt.DS_KEY_TARGET = ds;
        }
        return LemonCnt.DS_KEY_TARGET;
    }
}
